package spider;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.net.ConnectException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.SecureRandom;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;

import net.sf.json.JSONObject;


public class CommonUtil {

	public static String REQ_GET = "GET";
	public static String REQ_POST = "POST";
	
	
	
	
	/**
	 * 发送https的请求
	 * @param requestUrl 请求地址
	 * @param requestMethod 请求方式（GET,POST）
	 * @param outputStr 提交的数据
	 * @return （通过JSONObject.get(key)的方式获取JSON对象的属性）
	 * 
	 */
	public static final JSONObject httpsRequest(String requestUrl,String requestMethod,String outputStr){
		
		JSONObject jsonObject = null;
		try {
			TrustManager[] tm = { new WeixinX509TrustManager()};
			
			SSLContext  ssLContext = SSLContext.getInstance("SSL", "SunJSSE");
			ssLContext.init(null, tm, new SecureRandom());
			
			//从上述SSLContext对象中得到SSLSocketFactory对象
			SSLSocketFactory ssf = ssLContext.getSocketFactory();
			
			URL url = new URL(requestUrl);
			HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
			conn.setSSLSocketFactory(ssf);
			
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			//设置请求方式
			conn.setRequestMethod(requestMethod);
			
			//当outputStr不为null时，向输出流写数据
			if(outputStr != null) {
				OutputStream outputStream = conn.getOutputStream();
				//设置编码格式
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}
			
			//从输入流读取返回内容
			BufferedReader bufferedReader = new BufferedReader(new 
					InputStreamReader(conn.getInputStream(), "UTF-8"));
			
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			
			//释放资源
			bufferedReader.close();
			conn.disconnect();
			
			jsonObject = JSONObject.fromObject(buffer.toString());
			
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		
		return jsonObject;
		
	}
	
	
public static final JSONObject httpRequest(String requestUrl,String requestMethod,String outputStr){
		
		JSONObject jsonObject = null;
		try {
			TrustManager[] tm = { new WeixinX509TrustManager()};
			
			
			URL url = new URL(requestUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			
			conn.setDoOutput(true);
			conn.setDoInput(true);
			conn.setUseCaches(false);
			//设置请求方式
			conn.setRequestMethod(requestMethod);
			
			//当outputStr不为null时，向输出流写数据
			if(outputStr != null) {
				OutputStream outputStream = conn.getOutputStream();
				//设置编码格式
				outputStream.write(outputStr.getBytes("UTF-8"));
				outputStream.close();
			}
			
			//从输入流读取返回内容
			BufferedReader bufferedReader = new BufferedReader(new 
					InputStreamReader(conn.getInputStream(), "UTF-8"));
			
			String str = null;
			StringBuffer buffer = new StringBuffer();
			while((str = bufferedReader.readLine()) != null) {
				buffer.append(str);
			}
			
			//释放资源
			bufferedReader.close();
			conn.disconnect();
			
			jsonObject = JSONObject.fromObject(buffer.toString());
			
		} catch (ConnectException e) {
			e.printStackTrace();
		} catch (Exception ex) {
			ex.printStackTrace();
		} 
		
		return jsonObject;
		
	}
	
	
	public static boolean isMobile(String mobiles){
//		Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0,5-9]))\\d{8}$");
		Pattern p = Pattern.compile("^((13[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\\d{8}$");
//		Pattern p = Pattern.compile("^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$");
		Matcher m = p.matcher(mobiles);  
		return m.matches();  
	}  
	
	

}











